package tools;

public class Bounds {
    private static final int GRIDSIZE = 32;

    public double x;
    public double y;
    public double width;
    public double height;

    public Bounds(double x, double y, double width, double height) {
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
    }

    /**
     * Creates a bounding box centred in the given cell.
     */
    public Bounds(int row, int col, double width, double height) {
        this.x = (col + 0.5) * GRIDSIZE - width / 2;
        this.y = (row + 0.5) * GRIDSIZE - height / 2;
        this.width = width;
        this.height = height;
    }

    public Bounds(Bounds bounds) {
        x = bounds.x;
        y = bounds.y;
        width = bounds.width;
        height = bounds.height;
    }

    /*
     * Pixel coordinate related functions
     */
    public double getMinX() {
        return x;
    }

    public double getMinY() {
        return y;
    }

    public double getCenterX() {
        return x + width / 2;
    }

    public double getCenterY() {
        return y + height / 2;
    }

    public double getMaxX() {
        return x + width;
    }

    public double getMaxY() {
        return y + height;
    }

    /*
     * Cell coordinate related functions
     */
    public int getMinRow() {
        return (int) (y / GRIDSIZE);
    }

    public int getMinCol() {
        return (int) (x / GRIDSIZE);
    }

    public int getMaxRow() {
        return (int) Math.ceil((y + height) / GRIDSIZE);
    }

    public int getMaxCol() {
        return (int) Math.ceil((x + width) / GRIDSIZE);
    }

    /**
     * Returns true if the bounding box intersects the given bounding
     * box.
     */
    public boolean intersects(Bounds other) {
        return x < other.x + other.width && x + width > other.x
                && y < other.y + other.height && y + height > other.y;
    }
}
